﻿using Scoot.Data;
using Scoot.Data.Members;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;

namespace Scoot.Services.Members
{
    public class MemberService : IMemberService
    {
        private IMemberRepository repo;

        public MemberService()
        {
			this.repo = new MemberRepository();
        }

		public ServiceResult Create(MemberModel member)
        {
			repo.Create(Transforms.ModelToEntity(member));
			// TODO: Model validation.
			return ServiceResult.Successful();
        }

		public ServiceResult Edit(Guid memberID, MemberModel member)
        {
			repo.Edit(memberID, Transforms.ModelToEntity(member));
			// TODO: Model validation.
			return ServiceResult.Successful();
        }

		public ServiceResult Retire(Guid memberID)
        {
			repo.Retire(memberID);
			// TODO: Model validation.
			return ServiceResult.Successful();
        }

        public MemberModel GetByID(Guid memberID)
        {
			var member = repo.GetQueryable()
							.Where(m => m.MemberPK == memberID)
							.Select(Transforms.EntityToModel)
							.Single();

			return member;
        }

        public IList<MemberModel> Find(Expression<Func<MemberModel, bool>> predicate)
        {
			var members = repo.GetQueryable()
							.Select(Transforms.EntityToModel)
							.Where(predicate)
							.ToList();

			return members;				
        }
    }
}
